/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.IOFImage');
(function() {

    var REPLACE_JPG_EXT = new RegExp("\\.jpg", "i");

    /**
    * xtype: asc-iofimage
    * @class IOFImage
    * @extends Ext.Component
    * @namespace net.autodata
    */
    var IOFImage = Ext.extend(Ext.BoxComponent, {

        autoEl: {
            tag: 'span',
            cls: 'iof-image',
            style: {
            	'display': 'inline-block',
            	'-moz-inline-box': 'inline-block'
            }
        },

        /**
        * @property iofSrc
        * @type String
        */
        iofSrc: '{0}://imageonthefly.autodatadirect.com/IMAGES?',

        /**
        * IOF image name
        * @property imageName
        * @type String
        */
        imageName: undefined,
        
        /**
         * shot id
         * @property shotId
         * @type int
         */
        shotId: undefined,
        
        /**
         * @property imageType
         * @type String
         */
        imageType: undefined,

        /**
        * @property usePng
        * @type bool
        */
        usePng: true,

        /**
        * @property imageWidth
        * @type int
        */
        imageWidth: undefined,

        /**
        * @property imageHeight
        * @type int
        */
        imageHeight: undefined,

        /**
        * @property alt
        * @type String
        */
        alt: undefined,

        /**
        * @property autoTrim
        * @type bool
        */
        autoTrim: true,

        /**
        * @property errorImageName
        * @type String
        */
        errorImageName: undefined,
        
        constructor: function (cfg)
        {
        	this.addEvents({
        		'select': true,
        		'deselect': true
        	})
        	IOFImage.superclass.constructor.apply(this, arguments);
        	
        	cfg = cfg || {};
        	Ext.apply(this, cfg);
        	this.initialConfig = cfg;
        },
        
        /**
         * @method createImage
         * @private
         */
         createImage: function() {
             var img = Ext.DomQuery.selectNode('img', this.el.dom);
             if (img) {
                 Ext.removeNode(img);
                 delete img;
             }

             var src = this.buildUrl();
             
             var isValidImageSrc = src !== Ext.BLANK_IMAGE_URL;
             
             img = { tag: 'img', src: src };
             
             if (isValidImageSrc && Ext.isIE6 && this.usePng) {
                 Ext.apply(img, {
                     src: Ext.BLANK_IMAGE_URL,
                     style: String.format('filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'{0}\', sizingMethod=\'scale\')', src)
                 });
             }
             
             if (this.imageWidth) {
            	 img.width = this.imageWidth;
             }
             
             if (this.imageHeight) {
            	 img.height = this.imageHeight;
             }
             
             Ext.copyTo(img, this, ['alt']);

             Ext.DomHelper.overwrite(this.el.dom, img);
         },

         /**
         * @method buildUrl
         * @private
         */
         buildUrl: function() {

             if (Ext.isEmpty(this.imageName)) {
                 return Ext.BLANK_IMAGE_URL;
             }

             var imageName = this.imageName;
             if (this.usePng) {
                 imageName = imageName.replace(REPLACE_JPG_EXT, ".png");
             }

             var params = {
                 IMG: imageName
             };

             if (this.imageWidth !== undefined) {
                 params['WIDTH'] = this.imageWidth;
             }

             if (this.imageHeight !== undefined) {
                 params['HEIGHT'] = this.imageHeight;
             }

             params['AUTOTRIM'] = this.autoTrim ? 1 : 0;

             if (this.errorImageName !== undefined) {
                 params['NOIMG'] = this.errorImageName;
             }

             return String.format(this.iofSrc, Ext.isSecure ? 'https' : 'http') + Ext.urlEncode(params);
         },
         
         /**
          * @method getImageName
          * @return {String} imageName
          */
         getImageName: function ()
         {
        	return this.imageName; 
         },
         
         /**
         * @method setImageName
         * @param {String} newName
         */
         setImageName: function (newName) {
             this.imageName = newName;
             this.createImage();
         },
         
         getErrorImageName: function ()
         {
        	 return this.errorImageName;
         },
         
         select: function ()
         {
        	this.setSelected(true);
         },
        
         deselect: function ()
         {
        	 this.setSelected(false)
         },
         
        setSelected: function (select)
        {
        	 this[select ? 'addClass' : 'removeClass']('iof-image-selected');
        	 this.fireEvent(select ? 'select' : 'deselect', this); 
        },
         
        /**
         * @param {Event} e
         * @private
         */
        onClick: function (e)
        {
        	if (e) {
        		e.preventDefault();
        	}
        	
        	if(e.button !== 0){
                return;
            }
        	
        	this.fireEvent('click', this, e);
        	if (this.handler) {
        		this.handler.call(this.scope || this, this, e);
        	} 
        },
        
        /**
        * @method onRender
        * @private
        */
        onRender: function() {
            IOFImage.superclass.onRender.apply(this, arguments);
            this.createImage();
            
            this.el.on('click', this.onClick, this);
        },
        
        /**
         * @private
         */
        onDestroy: function ()
        {
        	IOFImage.superclass.apply(this, arguments);
        	this.el.un('click', this.onClick, this);
        }
        
    });

    Ext.reg('asc-iofimage', IOFImage);
    net.autodata.IOFImage = IOFImage;

})();